package com.toft.widgets.workflow.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jbpm.api.JbpmException;

import com.toft.widgets.workflow.dynamicnode.DynamicNode;
import com.toft.widgets.workflow.dynamicnode.DynamicNodeDao;
import com.toft.widgets.workflow.service.IRegulationService;
import com.toft.widgets.workflow.service.WorkflowFacade;
import com.toft.widgets.workflow.taskrole.Assignable;
import com.toft.widgets.workflow.taskrole.TaskRole;
import com.toft.widgets.workflow.taskrole.TaskRoleDao;

/**
 * 规则服务接口实现
 * 
 * @author 邹庆林
 * @version 1.0
 */
public class RegulationServiceImpl implements IRegulationService {

	public List getDynamicTaskCreateRule(String code) {

		List dynamicTaskRule = new ArrayList();
		try {
			DynamicNodeDao dynamicNodeDao = new DynamicNodeDao();
			DynamicNode dynamicNode = dynamicNodeDao.findDynamicNodeByCode(code, WorkflowFacade.getConnection());
			dynamicTaskRule = dynamicNode.getNodes(new HashMap(), WorkflowFacade.getConnection());
		} catch (Exception e) {
			throw new JbpmException("获取动态节点参与者规则[" + code + "]错误");
		}

		return dynamicTaskRule;

	}

	public Assignable getDynamicTaskParticipatorRule(String code, String agencyId, String userCode) {
		Assignable assignable = null;
		Map replacer = new HashMap();
		replacer.put("agencyId", agencyId);
		replacer.put("userCode", userCode);
		try {
			TaskRoleDao taskRoleDao = TaskRoleDao.getInstance();
			TaskRole taskRole = taskRoleDao.findTaskRoleByTaskRoleCode(code, agencyId, null, WorkflowFacade.getConnection());
			assignable = taskRole.getAssignable(replacer, WorkflowFacade.getConnection());
		} catch (Exception e) {
			throw new JbpmException("获取动态节点参与者规则[" + code + "]错误");
		}

		return assignable;

	}

	public Assignable getTaskParticipatorRule(String code, String agencyId, String departmentId, String userCode) {
		Assignable assignable = null;
		Map replacer = new HashMap();
		replacer.put("agencyId", agencyId);
		replacer.put("userCode", userCode);
		try {
			TaskRoleDao taskRoleDao = TaskRoleDao.getInstance();
			TaskRole taskRole = taskRoleDao.findTaskRoleByTaskRoleCode(code, agencyId, departmentId, WorkflowFacade.getConnection());
			assignable = taskRole.getAssignable(replacer, WorkflowFacade.getConnection());
		} catch (Exception e) {
			throw new JbpmException("获取参与者规则[" + code + "]错误");
		}

		return assignable;

	}

	public List getCounterSignature(String code, String agencyId, String departmentId, String userCode) {
		List counterSignatureUsers = new ArrayList();
		Map replacer = new HashMap();
		replacer.put("agencyId", agencyId);
		replacer.put("userCode", userCode);
		try {
			TaskRoleDao taskRoleDao = TaskRoleDao.getInstance();
			TaskRole taskRole = taskRoleDao.findTaskRoleByTaskRoleCode(code, agencyId, departmentId, WorkflowFacade.getConnection());

			Assignable assignable = taskRole.getAssignable(replacer, WorkflowFacade.getConnection());
			counterSignatureUsers = assignable.getUsers(agencyId, departmentId, WorkflowFacade.getConnection());
		} catch (Exception e) {
			throw new JbpmException("获取会签参与者规则[" + code + "]错误");
		}
		if (counterSignatureUsers.size() == 0 || counterSignatureUsers.isEmpty()) {
			throw new RuntimeException("获取下级处理人出错，请检查参与者规则[" + code + "]是否存在有效用户！");
		}

		return counterSignatureUsers;
	}

}
